<template>
  <div class="subject-manage">
    <div class="title">
      <div class="block"></div>
      <div class="word">专业列表</div>
      <div class="button">
        <el-button class="el-button" @click="refresh">刷新</el-button>
      </div>
    </div>
    <div>
      <search :formInline="formInline" @getSearchList="getSearchList">
        <div class="search-form">
          <el-form
            :rules="rules"
            ref="formInline"
            :inline="true"
            :model="formInline"
            class="demo-form-inline"
          >
            <el-form-item class="el-form-item" label="学校 :" prop="schoolId">
              <el-input
                class="el-input"
                v-model.trim="formInline.schoolId"
                placeholder="学校编号"
              ></el-input>
            </el-form-item>
            <el-form-item label="专业 :">
              <el-input
                class="el-input"
                v-model.trim="formInline.majorName"
                placeholder="专业名称"
              >
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button>搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </search>
    </div>
    <div>
      <datashow>
        <template v-slot:button>
          <div style="margin-right:20px">
            <el-button class="add-button" @click="goSubjectAdd">添加</el-button>
            <!-- <el-button class="delete-button">删除</el-button> -->
          </div>
        </template>
        <template v-slot:form>
          <el-table
            class="el-table"
            ref="multipleTable"
            :data="tableData"
            border
            style="width: 100%"
          >
            <el-table-column type="selection"></el-table-column>
            <el-table-column prop="schoolId" label="编号"> </el-table-column>
            <el-table-column prop="school" label="所属学校"> </el-table-column>
            <el-table-column prop="majorCName" label="专业中文名称">
            </el-table-column>
            <el-table-column prop="majorEName" label="专业英文名称">
            </el-table-column>
            <el-table-column prop="educationalSystem" label="学制">
            </el-table-column>
            <el-table-column prop="tuition" label="学费/年">
              <template slot-scope="scope">
                ￡{{ scope.row.tuition }}
              </template>
            </el-table-column>
            <el-table-column prop="proportion" label="国际生比例">
            </el-table-column>
            <el-table-column prop="operation" label="操作">
              <template slot-scope="scope">
                <el-button type="text" @click="goEdit(scope.row)" size="small"
                  >编辑</el-button
                >
                <el-button type="text" size="small" @click="deleteMajor(scope.row)">删除</el-button>
                <el-dialog title="提示"
                           :visible.sync="deleteMajorDialogVisible"
                           width="30%"
                           center>
                  <div style="text-align:center">
                    <span>是否确认删除专业中文名称为{{ majorCName }}的专业</span>
                  </div>
                  <span slot="footer"
                        class="dialog-footer">
                    <el-button class="el-button"
                               @click="deleteMajorDialogVisible = false">取 消</el-button>
                    <el-button class="el-button"
                               type="primary"
                               @click="goDeleteMajor()">确 定</el-button>
                  </span>
                </el-dialog>
              </template>
            </el-table-column>
          </el-table>

          <div class="bottom">
            <!-- <el-checkbox class="el-checkbox" @change="toggleSelect(tableData)"
              >全选</el-checkbox
            >
            <el-button class="el-button">批量操作</el-button>
            <el-button class="el-button">确定</el-button> -->
            <div class="paging"> 
            <paging
              class="paging"
              :total="total"
              @size="size"
              @currentpage="currentpage"
            ></paging>
            </div>
          </div>
        </template>
      </datashow>
    </div>

    <div>
      <footerinfo></footerinfo>
    </div>
  </div>
</template>

<script>
import search from "../../../component/search/search.vue";
import datashow from "../../../component/datashow/datashow.vue";
import paging from "../../../component/paging/paging.vue";
import footerinfo from "../../../component/footer/footer.vue";
import { getAllSubject, getMajor,deleteMajor } from "../../../api/index";
export default {
  inject: ["reload"],
  name: "schoolManage",
  components: {
    search, //筛选查询组件
    datashow, //数据列表组件
    paging, //分页组件
    footerinfo, //页脚组件
  },

  data() {
    return {
      current: "",
      pagesize: "",
      total: 0, //一共多少条数据
      majorCName: '',
      mid: '',
      deleteMajorDialogVisible: false,
      formInline: {
        schoolId: "", //学校编号或id
        majorName: "", //专业
      },
      tableData: [],
      // ruleForm: {
      //   school: ''
      // },
      rules: {
        schoolId: [
          { required: true, message: "请输入学校编号", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
    };
  },

  mounted() {
    this.$emit("routeName", "subjectList");
    this.getList();
  },

  methods: {
    currentpage(val) {
      // console.log(val);
      this.current = val; //得到新的当前页
      this.getList(); //获取新的数据列表
    },
    size(val) {
      // console.log(val);
      this.pagesize = val; //得到新当前页的一共几行数据
      this.getList(); //获取新的数据列表
    },
    refresh() {
      this.reload();
    },
    getList() {
      getAllSubject(this.current, this.pagesize).then((data) => {
        console.log(data.data);
        this.tableData = data.data.records;
        this.total = data.data.total;
      });
    },
    //获取搜索框传参后的数据列表，学校编号schoolId必传
    getSearchList() {
      if (this.formInline.schoolId) {
        this.current = ""; //清空分页当前页数
        this.pagesize = ""; //清空分页当前页的条数
        getMajor(
          this.formInline.schoolId,
          this.formInline.majorName,
          this.current,
          this.pagesize
        ).then((data) => {
          console.log(data.data);
          this.tableData = data.data.records;
          this.total = data.data.total;
        });
      }
      if (!(this.formInline.schoolId)) {
        this.getList()
      }
    },
    goSubjectAdd() {
      this.$router.push({
        name: "subjectAdd",
        params: {
          // val: val,
        },
      });
    },
    goEdit(val) {
      this.$router.push({
        name: "subjectEdit",
        params: {
          val: val,
        },
      });
    },
    deleteMajor(val) {
      console.log(val.mid);
      this.majorCName = val.majorCName
      this.mid = val.mid
      this.deleteMajorDialogVisible = true
    },
    goDeleteMajor() {
      this.deleteMajorDialogVisible = false
      console.log(this.mid);
      deleteMajor(this.mid).then((data) => {
        console.log(data)
        this.getList()
      })
    },
    toggleSelect(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect);
        });
        this.allSelect = !this.allSelect;
      }
    },
  },
};
</script>

<style scoped>
.title {
  position: relative;
  display: flex;
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #e4e4e4;
  line-height: 50px;
  background-color: #f3f3f3;
}
.block {
  width: 5px;
  height: 30px;
  margin-top: 10px;
  margin-left: 5px;
  background-color: #1abc9c;
}
/* 标题文字 */
.word {
  width: 100px;
  margin-left: 5px;
  color: #999999;
}
.button {
  position: absolute;
  right: 30px;
  margin-left: 80%;
}
.search-form {
  padding-top: 10px;
  padding-left: 10px;
}
.img {
  width: 40px;
  height: 40px;
}
.paging {
  text-align: center;
  margin: 20px 0px;
}
.bottom {
  /* display: flex; */
  text-align: center;
  margin: 10px 10px;
}
.el-checkbox {
  margin-top: 10px;
}
.el-button {
  margin-left: 20px;
}
.add-button {
  width: 100px;
  height: 40px;
}
.delete-button {
  margin-right: 10px;
  width: 100px;
  height: 40px;
}
</style>
